import { Controller, Get, Post, Body, Patch, Param, Delete, HttpException, HttpStatus, ParseIntPipe, HttpCode } from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { ForbiddenException } from 'src/exception/forbidden.exception';

@Controller('posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) { }

  @Post()
  create(@Body() createPostDto: CreatePostDto) {
    return this.postsService.create(createPostDto);
  }

  @Get()
  async findAll() {
    try {
      return  await this.postsService.findAll();
      // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
      // throw new ForbiddenException();
      // throw new BadRequestException('Something bad happened', { cause: new Error(), description: 'Some error description' }) // 自带的异常处理
    } catch (error) {
      //  throw new HttpException({
      //   status: HttpStatus.FORBIDDEN,
      //   error: 'This is a custom message',
      // }, HttpStatus.FORBIDDEN, {
      //   cause: "error" + error
      // });
      throw new ForbiddenException();
    }
  }

  @Get(':id')
  // findOne(@Param('id',ParseIntPipe) id: string) {
  findOne(@Param('id',new ParseIntPipe({errorHttpStatusCode:HttpStatus.NOT_ACCEPTABLE})) id: Number) {
    return this.postsService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updatePostDto: UpdatePostDto) {
    return this.postsService.update(+id, updatePostDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.postsService.remove(+id);
  }
}
